package xyf.p11.other.twopointer;

/**
 * ClassName: M1711_WordDistance
 * Description:
 * Author: xyf
 * Date: 9/9/22 10:23 AM
 * Version: 1.0
 **/
public class M1711_WordDistance
{
    public int findClosest(String[] words, String word1, String word2)
    {
        int index1 = -1;
        int index2 = -1;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < words.length; i++)
        {
            if (words[i].equals(word1))
            {
                index1 = i;
            }
            if (words[i].equals(word2))
            {
                index2 = i;
            }
            if (index1 != -1 && index2 != -1)
            {
                min = Math.min(min, Math.abs(index1 - index2));
            }
        }
        return min;
    }
}